// Category 模型编辑
const mongoose = require('mongoose')

const schema = new mongoose.Schema({
  name: { type:String },
  // type表示数据库里的ObjectId，ref表示关联的哪一个模型（在这为本身）
  parent: { type: mongoose.SchemaTypes.ObjectId, ref: 'Category'},
})


schema.virtual('children', {
  localField: '_id',
  foreignField: 'parent',
  justOne: false,
  ref: 'Category'
})

schema.virtual('newsList', {
  localField: '_id',
  foreignField: 'categories',
  justOne: false,
  ref: 'Article'
})


module.exports = mongoose.model('Category',schema)